<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page import="java.util.Set,java.util.List,com.crm.entity.Executives,com.crm.entity.UserRoles,com.crm.entity.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<link href="<%=application.getContextPath()%>/css/css-crm.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery-1.7.2.min.js"></script>
<script language="javaScript" src="<%=application.getContextPath()%>/js/jquery-ui-1.8.20.custom.min.js"></script>

<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery-1.6.min.js"></script>	
<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery-ui-1.8.17.custom.min.js"></script>	
<link rel="stylesheet" href="<%=application.getContextPath()%>/css/jquery-ui-1.8.17.custom.css"/>
	
<script language="JavaScript" src="<%=application.getContextPath()%>/js/gen_validatorv4.js" type="text/javascript" ></script>
<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery.validate.js"></script>	
<script type="text/javascript" src="<%=application.getContextPath()%>/js/jquery.validate.min.js"></script>
<script type="text/javascript">
function unameCheck(){
var name=$("#unameid").val();
if(name!=null){
	<%
	if(request.getAttribute("usersList")!=null){
		List<User> lu=(List<User>)request.getAttribute("usersList");
		for(User us:lu){%>
			var dbName="<%=us.getUserName()%>";
			alert(dbName);
			if(dbName==name){
				$("#userMsg").show();
				$("#unameid").focus();
				return false;
			}
		<%}
	}
	%>
	$("#userMsg").hide();
}
}
</script>
</head>

<body>
<div id="main">

  <!-- <div id="header"></div> -->
   <%@ include file="common/header.jsp" %>
  <%@ include file="common/submenu.jsp" %>

  <div id="content">
    <div id="resultMsg" class="result_msg" >
    <% 
    	if(request.getParameter("msg")!=null){
    		out.println(request.getParameter("msg"));
    	}
    	
    %>
    </div>
    <div id="userMsg" class="result_msg" style="display:none" >
   UserName Already exists
    </div>
 <div id="saveExec">
	<form:form id="form1" name="form1" method="post" action="save.do">

    <div id="work-area">
    
      <div class="heading">Users</div>
	  
      <div class="box">
        
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="left" valign="middle">&nbsp;</td>
              <td align="left" valign="middle">&nbsp;</td>
              <td align="left" valign="middle">&nbsp;</td>
              </tr>
            <tr>
              <td width="3%" align="left">&nbsp;</td>
              <td width="17%" align="left">First Name</td>
              <td width="35%" align="left" valign="middle">
              <form:input path="firstName" name="fName" type="text" class="form" id="fNameId" />
              </td>
              <td width="17%" align="left" valign="middle">Last Name</td>
              <td width="28%" align="left" valign="middle"><label></label>    
              <form:input path="lastName" name="lName" type="text" class="form" id="lNameId" />  </td>    
              </tr>
            <tr>
              <td align="right"></td>
              <td height="5" align="right"></td>
              <td height="5" align="right" valign="top"></td>
              <td height="5" align="left" valign="middle"></td>
              <td height="5" align="left" valign="top"></td>
              </tr>
            <tr>
              <td align="right"></td>
              <td height="5" align="right"></td>
              <td height="5" align="right" valign="top"></td>
              <td height="5" align="left" valign="middle"></td>
              <td height="5" align="left" valign="top"></td>
              </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="left">Email</td>
              <td align="left" valign="middle"><label>
                <form:input path="email" name="email" type="text" class="form-lowercase" id="emailid" />
                </label>              </td>
              <td align="left" valign="middle">Email Password</td>
              <td align="left" valign="middle"><label>
                <form:input path="emailPwd" name="emailPwd" type="password" class="form" id="emailPwdId" />
                </label>              </td>
              </tr>
            <tr>
              <td align="right"></td>
              <td height="5" align="right"></td>
              <td height="5" align="right" valign="top"></td>
              <td height="5" align="left" valign="middle"></td>
              <td height="5" align="left" valign="top"></td>
              </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="left">Role</td>
              <td align="left" valign="middle"><label>
                <form:select path="role" name="roles" class="form">
                <option value="">Please Select</option>
                <%
                if(request.getAttribute("roles")!=null){
                	List<UserRoles> uer=(List<UserRoles>)request.getAttribute("roles");
                	for(int i=0;i<uer.size();i++){
                %>
                <option value="<%=uer.get(i).getId()%>"><%=uer.get(i).getRole()%></option>
                <%
                	}
                }
                %>
                </form:select>
                </label>              </td>
              <td align="left" valign="middle">User Name</td>
              <td align="left" valign="middle"><label>
                <form:input path="userName" name="uName" type="text" class="form" id="unameid" onblur="javascript:return unameCheck();"/>
                </label>              </td>
              </tr>
            <tr>
              <td align="right"></td>
              <td height="5" align="right"></td>
              <td height="5" align="right" valign="top"></td>
              <td height="5" align="left" valign="middle"></td>
              <td height="5" align="left" valign="top"></td>
              </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="left">Password</td>
              <td align="left" valign="middle"><label>
                <form:input path="password" name="password" type="password" class="form" id="passwordId" />
                </label>              </td>
              <td align="left">Confirm Password</td>
              <td align="left" valign="top">
              <input name="confPwd" type="password" class="form" id="confPwdId" /></td>
              </tr>
            <tr>
              <td align="right"></td>
              <td height="5" align="right"></td>
              <td height="5" align="right" valign="top"></td>
              <td height="5" align="left" valign="middle"></td>
              <td height="5" align="left" valign="top"></td>
              </tr>
            <tr>
              <td align="left">&nbsp;</td>
              <td align="left">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="middle">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              </tr>
            <tr>
              <td colspan="3" align="left" valign="middle"></td>
              <td align="left" valign="middle">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
              </tr>
          </table>
     
      </div>
	  
	  <div class="box2"></div>
	  <div class="box2">
	    <div class="buttton-main">
	    <input type="reset" value="Clear" class="button"/>
	     <div class="button" onclick="deleteUser();">Delete</div>
	    <!-- <div class="button"><a href="edit.do">Edit</a></div>  -->
	     <input type="submit" class="button" value="Save" onclick="javascript:return validate();"/>
	    </div>
	  </div>
	  
    </div>

    </form:form>

    </div>
   
    <!-- InstanceEndEditable -->
    </div>
    <div id="deleteDiv" style="display:none" title="Delete Users">
<form name="DeleteForm" method="post" action="<%=application.getContextPath()%>/users/delete.do ">
Executive Name:
<select name="userId" id="delUser">
<option value="">Please Select</option>
</select>
<input type="submit" class="button" value="Delete"/>
</form>
</div>
     <div id="footer"></div>  
</div>
</body>

</html>
<script>

	$(function() {
		$( "#dobID" ).datepicker({ dateFormat: "dd-mm-yy" },"option", "autoSize", true);
		$( "#joinDateId" ).datepicker({ dateFormat: "dd-mm-yy" },"option", "autoSize", true);
	});
	</script>
<script type="text/javascript">
 $("#exeNameId").change(function(){
	var data=$('#exeNameId').val();
	$.ajax({
		url:"<%=application.getContextPath()%>/executives/search.do" ,
	  	data:"execName="+data,
	  	success:function(res){
	  		 //$('#companyForm').ajaxForm(options);
	  		$('#saveExec').remove();
	  		$("#content").append('<div id="editExec"></div> ');
	  		$('#editExec').html(res);
			
	  	}
	  });	
});
function validate(){
	var name=$("#execNameId").val();
	var website=$("#websiteid").val();
	var email=$("#emailid").val();
	var fax=$("#faxid").val();
	var phone=$("#phoneid").val();
	var country=$("#countryid").val();
	var address=$("#addressid").val();
	var cell=$("#cellid").val();
	
	/* if(name==null || name==""){
		jAlert("Please Enter Executive Name");
		$("#execNameId").focus();
		return false;
	} */
	/* if(website==null || website==""){
		jAlert("Please Enter Website");
		$("#websiteid").focus();
		return false;
	} */
	/* var emailFormat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	 if(emailFormat.test(email) == false) {
	 
	      jAlert('Invalid Email Address');
	      $("#email").focus();
	      return false;
	   } */
}
function deleteUser(){
	$.ajax({
		url:"<%=application.getContextPath()%>/users/getAllUsers.do",
		mimetype: "text/plain",
		cache:false,
		success: function(res){
			var e2=document.getElementById('delUser');
			$('#delUser')
		    .find('option')
		    .remove()
		    .end()
		   	;
			var temp=res.split(",");
			var o=document.createElement('option');
			o.value="Please Select";
			o.text="Please Select";
			e2.options.add(o);
			if(temp != null){
				for(var i=0;i<temp.length-1;i++){
					var o=document.createElement('option');
					var t=temp[i].split("@");
					o.value=t[1];
					o.text=t[0];
					e2.options.add(o);
				}
			}
			
			
		}

	});
	
	var $dialog = $('#deleteDiv').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        height: 100
      				
    });
		 
    $dialog.dialog('open');	
}
</script>

